﻿using cnBlogs.DataModel;
using cnBlogs.Model;
using cnBlogs.ViewModel;
using System;
using System.Collections.Generic;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.UI.Xaml.Controls;

// The Item Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234232

namespace cnBlogs.Views
{
    /// <summary>
    /// A page that displays details for a single item within a group while allowing gestures to
    /// flip through other items belonging to the same group.
    /// </summary>
    public sealed partial class ArticleDetail : cnBlogs.Common.LayoutAwarePage
    {
        public ArticleDetail()
        {
            this.InitializeComponent();
        }

        private DataTransferManager dataTransferManager;
        private ArticleInfo _articleinfo;
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            this.dataTransferManager = DataTransferManager.GetForCurrentView();
            this.dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
            // Allow saved page state to override the initial item to display
            //if (pageState != null && pageState.ContainsKey("SelectedItem"))
            //{
            //    navigationParameter = pageState["SelectedItem"];
            //}

            // TODO: Assign a bindable group to this.DefaultViewModel["Group"]
            // TODO: Assign a collection of bindable items to this.DefaultViewModel["Items"]
            // TODO: Assign the selected item to this.flipView.SelectedItem
            _articleinfo = navigationParameter as ArticleInfo;
            Original.Content = _articleinfo.Link;
            Original.NavigateUri = new Uri(_articleinfo.Link);
            NewsInfo newsinfo = navigationParameter as NewsInfo;

            if (newsinfo == null)
            {
                ArticleDetailViewMoel advm = new ArticleDetailViewMoel(_articleinfo);
                await advm.GetResource();
                this.DefaultViewModel["ArticleDetail"] = advm.ArticleDetailInfo;
                webView.NavigateToString(advm.ArticleDetailInfo.ArticleContent);

            }
            else
            {
                NewsDetailViewModel ndvm = new NewsDetailViewModel(newsinfo);
                await ndvm.GetResource();
                this.DefaultViewModel["ArticleDetail"] = ndvm.NewsDetailInfo;
                webView.NavigateToString(ndvm.NewsDetailInfo.ArticleContent);
                Original.Content = newsinfo.Link;
            }

        }

        private void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            DataPackage requestData = args.Request.Data;
            requestData.Properties.Title = _articleinfo.Title;
            requestData.Properties.Description = _articleinfo.PublishTime;
            requestData.SetText(_articleinfo.Overview + _articleinfo.Link);
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
            this.dataTransferManager.DataRequested -= new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
            //var selectedItem = this.flipView.SelectedItem;
            // TODO: Derive a serializable navigation parameter and assign it to pageState["SelectedItem"]
        }
    }
}
